package cn.jvav.zh.ui;

import java.util.InputMismatchException;
import java.util.List;
import java.util.Scanner;
import java.util.stream.IntStream;

public class Menu {
	public List<MenuItem> options;
	public Menu(){}
	public Menu(List<MenuItem> options){
		this.options = options;
	}
	public void showMenu(){
		System.out.println("-1:退出");
		IntStream.range(0, options.size())
				.forEach(i -> System.out.println(i + ":" + options.get(i).description));
	}
	public void choose(){
		var scanner = new Scanner(System.in);
		while (true){
			// 清屏
//			System.out.print("\033[H\033[2J");
			showMenu();
			int index;
			try{
				index = scanner.nextInt();
			}catch (InputMismatchException e){
				// 清空输入缓冲
				scanner.next();
				continue;
			}
			if (index == -1) {
				break;
			}
			if (index >= 0 && index < options.size()) {
				options.get(index).option.option();
				continue;
			}
			System.out.println("请选择有效选项");
		}
	}
}